﻿using Gentings.Data.Migrations;

namespace Gentings.Extensions.Tagables
{
    /// <summary>
    /// 数据库迁移基类。
    /// </summary>
    /// <typeparam name="TTag">标签类型。</typeparam>
    /// <typeparam name="TTagIndex">标签索引类型。</typeparam>
    public abstract class DataMigration<TTag, TTagIndex> : DataMigration
        where TTag : TagBase, new()
        where TTagIndex : TagIndexBase, new()
    {
        /// <summary>
        /// 当模型建立时候构建的表格实例。
        /// </summary>
        /// <param name="builder">迁移实例对象。</param>
        public override void Create(MigrationBuilder builder)
        {
            builder.CreateTable<TTag>(table => table
                .Column(x => x.Id)
                .Column(x => x.Name)
                .Column(x => x.Count)
                .Column(x => x.Letters)
                .Column(x => x.IsConfirm)
                .UniqueConstraint(x => x.Name)
            );

            builder.CreateTable<TTagIndex>(table => table
                .Column(x => x.IndexedId)
                .Column(x => x.TagId)
                .ForeignKey<TTag>(x => x.TagId, x => x.Id, onDelete: ReferentialAction.Cascade)
            );
        }
    }
}
